package xin.nick.demo;

import feign.Contract;
import feign.Logger;
import feign.Request;
import feign.auth.BasicAuthRequestInterceptor;
import okhttp3.ConnectionPool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class FeignConfiguration {

    //
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    // 设置为原生feign
//    @Bean
//    public Contract feignContract() {
//        return new feign.Contract.Default();
//    }

    // 设置 Basic 认证方式
    @Bean
    public MyFeignBasicAuthRequestInterceptor basicAuthRequestInterceptor() {
//        return new BasicAuthRequestInterceptor("Nick", "123456");
        return new MyFeignBasicAuthRequestInterceptor();
    }

    // 超时时间 默认10×1000,60×1000
    @Bean
    public Request.Options options() {
        return new Request.Options(5000, 10000);
    }

    // 配置 client
    @Bean
    public okhttp3.OkHttpClient okHttpClient(){
        return new okhttp3.OkHttpClient.Builder()
                //设置连接超时
                .connectTimeout(60, TimeUnit.SECONDS)
                //设置读超时
                .readTimeout(60, TimeUnit.SECONDS)
                //设置写超时
                .writeTimeout(60,TimeUnit.SECONDS)
                //是否自动重连
                .retryOnConnectionFailure(true)
                .connectionPool(new ConnectionPool())
                //构建OkHttpClient对象
                .build();
    }

    // 编码器,解码器
//    @Bean
//    public Decoder decoder() {
//        return new MyDecoder();
//    }
//    @Bean
//    public Encoder encoder() {
//        return new MyEncoder();
//    }
}
